package com.ruoyi.visitor.controller;

import java.util.List;

import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.visitor.service.impl.VisitorAppService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.visitor.domain.VisitorAppointment;
import com.ruoyi.visitor.service.IVisitorAppointmentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 访客预约记录Controller
 * 
 * @author ruoyi
 * @date 2025-04-12
 */
@Controller
@RequestMapping("/visitor/appointment")
public class VisitorAppointmentController extends BaseController
{
    private String prefix = "visitor/appointment";

    @Autowired
    private IVisitorAppointmentService visitorAppointmentService;
    @Autowired
    private VisitorAppService visitorAppService;

    //@RequiresPermissions("system:appointment:view")
    @GetMapping("/approval")
    public String approval()
    {
        return prefix + "/approval";
    }


    /**
     * 访客统计
     * @return
     */
    @GetMapping("/list")
    public String list()
    {
        return prefix + "/appointment";
    }


    @GetMapping()
    public String appointment()
    {
        return prefix + "/appointment";
    }
    /**
     * 查询访客预约记录列表
     */
    //@RequiresPermissions("system:appointment:list")
    @PostMapping("/toApprovalList")
    @ResponseBody
    public TableDataInfo toApprovalList(VisitorAppointment visitorAppointment)
    {
        startPage();
        visitorAppointment.setVisitedPhone(ShiroUtils.getSysUser().getPhonenumber());
        List<VisitorAppointment> list = visitorAppointmentService.selectVisitorAppointmentList(visitorAppointment);
        return getDataTable(list);
    }

    /**
     * 访客统计
     * @param visitorAppointment
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(VisitorAppointment visitorAppointment)
    {
        startPage();
        List<VisitorAppointment> list = visitorAppointmentService.selectVisitorAppointmentList(visitorAppointment);
        return getDataTable(list);
    }

    /**
     * 导出访客预约记录列表
     */
    //@RequiresPermissions("system:appointment:export")
    @Log(title = "访客预约记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(VisitorAppointment visitorAppointment)
    {
        List<VisitorAppointment> list = visitorAppointmentService.selectVisitorAppointmentList(visitorAppointment);
        ExcelUtil<VisitorAppointment> util = new ExcelUtil<VisitorAppointment>(VisitorAppointment.class);
        return util.exportExcel(list, "访客预约记录数据");
    }

    /**
     * 新增访客预约记录
     */
    //@RequiresPermissions("system:appointment:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存访客预约记录
     */
    //@RequiresPermissions("system:appointment:add")
    @Log(title = "访客预约记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(VisitorAppointment visitorAppointment)
    {
        return toAjax(visitorAppointmentService.insertVisitorAppointment(visitorAppointment));
    }

    /**
     * 修改访客预约记录
     */
    //@RequiresPermissions("system:appointment:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        VisitorAppointment visitorAppointment = visitorAppointmentService.selectVisitorAppointmentById(id);
        mmap.put("visitorAppointment", visitorAppointment);
        return prefix + "/edit";
    }

    /**
     * 修改保存访客预约记录
     */
    //@RequiresPermissions("system:appointment:edit")
    @Log(title = "访客预约记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(VisitorAppointment visitorAppointment)
    {
        return toAjax(visitorAppointmentService.updateVisitorAppointment(visitorAppointment));
    }

    /**
     * 删除访客预约记录
     */
    //@RequiresPermissions("system:appointment:remove")
    @Log(title = "访客预约记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(visitorAppointmentService.deleteVisitorAppointmentByIds(ids));
    }

    /**
     * 审核
     * @param visitorAppointment
     * @return
     */
    @PostMapping("/approval")
    @ResponseBody
    public AjaxResult approval(VisitorAppointment visitorAppointment)
    {
        return toAjax(visitorAppService.visitorApproval(visitorAppointment.getId(),visitorAppointment.getApprovalStatus()));
    }
}
